package com.opal.jx.service.impl;

import com.opal.consts.WSC;
import com.opal.domain.R;
import com.opal.jx.JxConst.JxConst;
import com.opal.jx.entity.JxAssessReception;
import com.opal.jx.entity.table.*;
import com.opal.jx.exception.JxAssessException;
import com.opal.jx.mapper.mysql.JxWLBMapper;
import com.opal.jx.service.JxWLBService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class JxWLBServiceImpl implements JxWLBService {
    @Autowired
    private JxWLBMapper jxWLBMapper;
    @Override
    public Object selectEmpAndDept(String pkDept) {
        List<JxEmp> jxEmps = jxWLBMapper.selectEmpAndDept(pkDept);
        Map resultMap= new HashMap();
        resultMap.put("total",jxEmps.size());
        resultMap.put("rows",jxEmps);
        //处理页脚行
        Map m= new HashMap();
        List footer=new ArrayList();
        m.put("empCode", "合计");
        m.put("resultMoney", "0");
        footer.add(m);
        resultMap.put("footer", footer);
        return resultMap;
    }

    @Override
    public Object queryJxAssessWlbPmcAndYearAndMonth(Integer year, Integer month,Integer type) {
        List<JxAssessWlbPmc> JxAssessWlbPmcs= jxWLBMapper.queryJxAssessWlbPmcAndYearAndMonth(year, month);
        Map resultMap= new HashMap();
        resultMap.put("total",JxAssessWlbPmcs.size());
        resultMap.put("rows",JxAssessWlbPmcs);
        //处理页脚行
        List<JxAssessWlbTotal> jxAssessWlbTotals = jxWLBMapper.queryJxAssessWlbTotalAndYearAndMonthAndType(year, month, type);
        String totalMoney="0";
        if (jxAssessWlbTotals.size()>0){
            totalMoney = jxAssessWlbTotals.get(0).getTotalMoney();
            totalMoney = StringUtils.isBlank(totalMoney)?"0":totalMoney;
        }
        Map m= new HashMap();
        List footer=new ArrayList();
        m.put("empCode", "合计");
        m.put("resultMoney", totalMoney);
        footer.add(m);
        resultMap.put("footer", footer);
        return resultMap;
    }

    @Override
    @Transactional("mysqlTransactionManager")
    public Object addPmcData(JxAssessReception jxAssessReception) {
        try{
            //删除当月数据
            List<JxAssessWlbTotal> jwt = jxAssessReception.getJxAssessWlbTotals();
            JxAssessWlbTotal jw = jwt.get(0);
            Integer year = jw.getAssessYear();
            Integer month = jw.getAssessMonth();
            Integer assessType = jw.getAssessType();
                //删除pmc
            jxWLBMapper.deletejxAssessWlbPmcByYearAndMonth(year, month);
                //删除物流部-合计
            jxWLBMapper.deletejxAssessWlbTotalByYearAndMonthAndType(year,month,assessType);
            //插入数据
                //插入物流部pmc数据
            List<JxAssessWlbPmc> jxAssessWlbPmcs = jxAssessReception.getJxAssessWlbPmcs();
            for (JxAssessWlbPmc pmc :jxAssessWlbPmcs){
                jxWLBMapper.insertPmcData(pmc);
            }
                //插入物流部-合计数据
            List<JxAssessWlbTotal> jxAssessWlbTotals = jxAssessReception.getJxAssessWlbTotals();

            for (JxAssessWlbTotal j:jxAssessWlbTotals){
                jxWLBMapper.insertWlbTotalData(j);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new JxAssessException(WSC.REQUEST_ERROR,"添加失败");
        }
        return new R(WSC.RESTPONSE_OK, JxConst.SUBMIT_SUCCESS);
    }

    @Override
    public List<JxEmp> selectCpkEmpByDeptAndNotInPostId(String pkDept) {
        return jxWLBMapper.selectCpkEmpByDeptAndNotInPostId(pkDept);
    }

    @Override
    public List<JxEmp> selectCpkEmpByDeptAndInPostId(String pkDept) {
        return jxWLBMapper.selectCpkEmpByDeptAndInPostId(pkDept);
    }

    //配件組
    @Override
    public List<JxEmp> selectPjzEmpNotZgAndZz(String pkDept) {
        return jxWLBMapper.selectPjzEmpNotZgAndZz(pkDept);
    }

    @Override
    public List<JxEmp> selectPjzEmpSoAsZgAndZz(String pkDept) {
        return jxWLBMapper.selectPjzEmpSoAsZgAndZz(pkDept);
    }

    @Override
    @Transactional("mysqlTransactionManager")
    public Object addPjzData(JxAssessReception jxAssessReception) {
        try{
            //删除当月数据
            List<JxAssessWlbTotal> jwt = jxAssessReception.getJxAssessWlbTotals();
            JxAssessWlbTotal jw = jwt.get(0);
            Integer year = jw.getAssessYear();
            Integer month = jw.getAssessMonth();
            Integer assessType = jw.getAssessType();
                //删除配件組
            jxWLBMapper.deletejxAssessWlbPjzByYearAndMonth(year, month);
                //删除物流部-合计
            jxWLBMapper.deletejxAssessWlbTotalByYearAndMonthAndType(year,month,assessType);
            //插入数据
                //插入物流部pjz数据
            List<JxAssessWlbPjz> jxAssessWlbPjzs = jxAssessReception.getJxAssessWlbPjzs();
            for (JxAssessWlbPjz pjz :jxAssessWlbPjzs){
                jxWLBMapper.insertPjzData(pjz);
            }
                //插入物流部-合计数据
            List<JxAssessWlbTotal> jxAssessWlbTotals = jxAssessReception.getJxAssessWlbTotals();

            for (JxAssessWlbTotal j:jxAssessWlbTotals){
                jxWLBMapper.insertWlbTotalData(j);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new JxAssessException(WSC.REQUEST_ERROR,"添加失败");
        }
        return new R(WSC.RESTPONSE_OK, JxConst.SUBMIT_SUCCESS);
    }

    @Override
    public Object queryJxAssessWlbCpk(Integer year, Integer month, Integer type) {
        Map<String,Object> map =new HashMap<>();
        //获取非管理员的数据
        List<JxAssessWlbCpk> jxAssessWlbCpk = jxWLBMapper.queryJxAssessWlbCpkAndYearAndMonthAndNotManager(year, month);
        //获取管理员的数据
        List<JxAssessWlbCpk> manager = jxWLBMapper.queryJxAssessWlbCpkAndYearAndMonthAndManager(year, month);
        //获取成品科合计数据
        List<JxAssessWlbTotal> jxAssessWlbTotals = jxWLBMapper.queryJxAssessWlbTotalAndYearAndMonthAndType(year, month, type);
        map.put("a", jxAssessWlbCpk);
        map.put("b",manager);
        String totalMoney="0";
        if (jxAssessWlbTotals.size()>0){
            totalMoney = jxAssessWlbTotals.get(0).getTotalMoney();
        }

        map.put("money",totalMoney);
        return map;
    }

    @Override
    public Object queryJxAssessWlbPjz(Integer year, Integer month, Integer type) {
        Map<String,Object> map =new HashMap<>();
        //获取非管理员的数据
        List<JxAssessWlbPjz> jxAssessWlbPjzs = jxWLBMapper.queryJxAssessWlbPjzAndYearAndMonthAndNotManager(year, month);
        //获取管理员的数据
        List<JxAssessWlbPjz> manager = jxWLBMapper.queryJxAssessWlbPjzAndYearAndMonthAndManager(year, month);
        //获取配件组合计数据
        List<JxAssessWlbTotal> jxAssessWlbTotals = jxWLBMapper.queryJxAssessWlbTotalAndYearAndMonthAndType(year, month, type);
        map.put("a", jxAssessWlbPjzs);
        map.put("b",manager);
        String totalMoney="0";
        if (jxAssessWlbTotals.size()>0){
             totalMoney = jxAssessWlbTotals.get(0).getTotalMoney();
        }

        map.put("money",totalMoney);
        return map;
    }

    @Override
    public List<JxEmp> selectPjzEmpNotZgAndCcAndQj(String pkDept) {
        return  jxWLBMapper.selectPjzEmpNotZgAndCcAndQj(pkDept);
    }

    @Override
    public List<JxEmp> selectPjzEmpSoAsZgAndCcAndQj(String pkDept) {
        return jxWLBMapper.selectPjzEmpSoAsZgAndCcAndQj(pkDept);
    }

    @Override
    public Object queryJxAssessWlbYcl(Integer year, Integer month, Integer type) {
        Map<String,Object> map =new HashMap<>();
        //获取非管理员的数据
        List<JxAssessWlbYcl> jxAssessWlbYcl = jxWLBMapper.queryJxAssessWlbYclAndYearAndMonthAndNotManager(year, month);
        //获取主管、叉车司机、清洁工的数据
        List<JxAssessWlbYcl> manager = jxWLBMapper.queryJxAssessWlbYclAndYearAndMonthAndManager(year, month);
        //获取配件组合计数据
        List<JxAssessWlbTotal> jxAssessWlbTotals = jxWLBMapper.queryJxAssessWlbTotalAndYearAndMonthAndType(year, month, type);
        map.put("a", jxAssessWlbYcl);
        map.put("b",manager);
        String totalMoney="0";
        if (jxAssessWlbTotals.size()>0){
            totalMoney = jxAssessWlbTotals.get(0).getTotalMoney();
        }

        map.put("money",totalMoney);
        return map;
    }

    @Override
    @Transactional("mysqlTransactionManager")
    public Object addYclData(JxAssessReception jxAssessReception) {
        try{
            //删除当月数据
            List<JxAssessWlbTotal> jwt = jxAssessReception.getJxAssessWlbTotals();
            JxAssessWlbTotal jw = jwt.get(0);
            Integer year = jw.getAssessYear();
            Integer month = jw.getAssessMonth();
            Integer assessType = jw.getAssessType();
            //删除原材料
            jxWLBMapper.deletejxAssessWlbYclByYearAndMonth(year, month);
            //删除物流部-合计
            jxWLBMapper.deletejxAssessWlbTotalByYearAndMonthAndType(year,month,assessType);
            //插入数据
            //插入物流部ycl数据
            List<JxAssessWlbYcl> jxAssessWlbYcls = jxAssessReception.getJxAssessWlbYcl();
            for (JxAssessWlbYcl ycl :jxAssessWlbYcls){
                jxWLBMapper.insertYlcData(ycl);
            }
            //插入物流部-合计数据
            List<JxAssessWlbTotal> jxAssessWlbTotals = jxAssessReception.getJxAssessWlbTotals();

            for (JxAssessWlbTotal j:jxAssessWlbTotals){
                jxWLBMapper.insertWlbTotalData(j);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new JxAssessException(WSC.REQUEST_ERROR,"添加失败");
        }
        return new R(WSC.RESTPONSE_OK, JxConst.SUBMIT_SUCCESS);
    }

    @Override
    @Transactional("mysqlTransactionManager")
    public Object addCpkData(JxAssessReception jxAssessReception) {
        try{
            //删除当月数据
            List<JxAssessWlbTotal> jwt = jxAssessReception.getJxAssessWlbTotals();
            JxAssessWlbTotal jw = jwt.get(0);
            Integer year = jw.getAssessYear();
            Integer month = jw.getAssessMonth();
            Integer assessType = jw.getAssessType();
            //删除成品科
            jxWLBMapper.deletejxAssessWlbCpkByYearAndMonth(year, month);
            //删除物流部-合计
            jxWLBMapper.deletejxAssessWlbTotalByYearAndMonthAndType(year,month,assessType);
            //插入数据
            //插入物流部成品科数据
            List<JxAssessWlbCpk> jxAssessWlbCpk = jxAssessReception.getJxAssessWlbCpk();
            for (JxAssessWlbCpk cpk :jxAssessWlbCpk){
                jxWLBMapper.insertCpkData(cpk);
            }
            //插入物流部-合计数据
            List<JxAssessWlbTotal> jxAssessWlbTotals = jxAssessReception.getJxAssessWlbTotals();

            for (JxAssessWlbTotal j:jxAssessWlbTotals){
                jxWLBMapper.insertWlbTotalData(j);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new JxAssessException(WSC.REQUEST_ERROR,"添加失败");
        }
        return new R(WSC.RESTPONSE_OK, JxConst.SUBMIT_SUCCESS);
    }
}
